package com.wyx;

import com.wyx.httpService.shanxitobaccoService.impl.SmokeInfoShanxiTobaccoCrawlerServiceImpl;
import com.wyx.pojo.Pic;
import com.wyx.pojo.ProductBeanResult;
import com.wyx.service.PicService;
import com.wyx.service.ProductBeanResultService;
import com.wyx.service.SmokeService;
import com.wyx.utils.commonUtil.CUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//下载图片  方式
@SpringBootTest
public class DownPic {
    @Autowired
    private SmokeInfoShanxiTobaccoCrawlerServiceImpl smokeInfoShanxiTobaccoCrawlerService;
    @Autowired
    private SmokeService smokeService;
    @Autowired
    private ProductBeanResultService productBeanResultService;

    @Autowired
    private PicService picService;

    @Test
    public void down() throws IOException {
        List<ProductBeanResult> list1 = productBeanResultService.list();
        ArrayList<Pic> pics = new ArrayList<>();
        for (ProductBeanResult productBeanResult : list1) {
            byte[] bytes = smokeInfoShanxiTobaccoCrawlerService.downHttpPic(productBeanResult.getImg_main());
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            BufferedImage image = ImageIO.read(bis);
            if (image != null) {
                String uuid = CUtil.getUUID();
                Pic pic = new Pic();
                pic.setId(uuid);
                pic.setObjId(productBeanResult.getSmokeId());
                pic.setPicAddress("src\\main\\resources\\smokePic\\" + uuid);
                pics.add(pic);
                // 保存图片到本地文件
                File output = new File("src\\main\\resources\\smokePic\\" + uuid + ".jpg");
                System.out.println(output.getPath());
                ImageIO.write(image, "jpg", output);
            }
            bis.close();
        }
        picService.saveBatch(pics);
    }
}
